<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar
      @click-left="onClickLeft"
      left-arrow
      title="编辑资料"
    ></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        @click="showNameDialog"
        is-link
        title="名称"
        :value="userInfo.name"
      />
      <van-cell
        is-link
        title="性别"
        @click="showGender = true"
        :value="userInfo.gender === 0 ? '男' : '女'"
      />
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="showBirthdayPop"
      />
    </van-cell-group>
    <van-dialog
      @confirm="savename"
      v-model="showName"
      title="修改昵称"
      show-cancel-button
    >
      <van-field ref="nameField" v-model="name" />
    </van-dialog>

    <van-popup v-model="showGender" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showGender = false"
      />
      <van-cell-group>
        <van-cell title="男" is-link @click="changeGender(0)" />
        <van-cell title="女" is-link @click="changeGender(1)" />
      </van-cell-group>
    </van-popup>
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        @confirm="confirmDate"
        @cancel="cancelDate"
        v-model="birthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <div class="cropper-box" v-show="isShowCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCropWidth="120"
        autoCropHeight="120"
        fixed
        autoCrop
      ></vueCropper>
      <van-button @click="submitPhoto" type="primary" class="confirm-btn"
        >确定</van-button
      >
      <van-button @click="isShowCropper = false" type="info" class="cancel-btn"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo, upload } from '../../api/edit'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      showName: false,
      name: '',
      showGender: false,
      showBirthday: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      birthday: new Date(2021, 0, 17),
      img: '',
      isShowCropper: false
    }
  },
  components: {
    VueCropper
  },
  methods: {
    submitPhoto () {
      this.$refs.cropper.getCropBlob(blob => {
        const data = new FormData()
        data.append('photo', blob)
        upload(data).then(data => {
          this.$toast.success('头像修改成功')
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: data.data.data.photo
          })
          this.isShowCropper = false
        })
      })
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      console.log(file)
      this.isShowCropper = true
      this.img = file.content
      // const data = new FormData()
      // data.append('photo', file.file)
      // upload(data).then(data => {
      //   this.$toast.success('头像修改成功')
      //   this.$store.commit('setUserInfo', {
      //     ...this.userInfo,
      //     photo: data.data.data.photo
      //   })
      // })
    },
    confirmDate () {
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(data => {
        this.$toast.success('生日修改成功')
        this.showBirthday = false

        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: data.data.data.birthday
        })
      })
    },
    cancelDate () {
      this.showBirthday = false
    },
    showBirthdayPop () {
      this.showBirthday = true
      this.birthday = new Date(this.userInfo.birthday)
    },
    changeGender (gender) {
      editUserInfo({ gender }).then(data => {
        this.$toast.success('性别修改成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender
        })
      })
      this.showGender = false
    },
    savename () {
      editUserInfo({ name: this.name }).then(data => {
        this.$toast.success('姓名修改成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: data.data.data.name
        })
      })
    },
    showNameDialog () {
      this.showName = true
      this.name = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.nameField.focus()
      })
    },
    onClickLeft () {
      this.$router.push({ path: '/my' })
    }
  },
  computed: mapState(['userInfo'])
  // computed: {
  //   userInfo () {
  //     return this.$store.state.userInfo
  //   }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  // .van-nav-bar {
  //   background-color: #3196fa;
  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  //   .van-icon-arrow-left {
  //     color: #fff;
  //   }
  //   .van-nav-bar__text {
  //     color: #fff;
  //   }
  // }
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    position: relative;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }
  .van-field {
    border: 1px solid #ccc;
  }
  .van-dialog__content {
    padding: 10px;
    box-sizing: border-box;
  }
  .van-uploader {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0;

    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
  // 裁切弹框样式
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
